import _ from 'lodash'
import nofund from './modules/base/404.vue'
import Config from './libs/config'
import zjApp from '@/modules/zjApp'
import ajzApp from '@/modules/ajzApp'
/* eslint-disable */

//自动化加载模块局部路由
let modules = (r => {
    var result = [];
    r.keys().forEach((key) => {
        let rs = r(key).default;
        if (_.isArray(rs)) {
            result = result.concat(rs);
        } else if (rs.components && _.isFunction(rs.components)) {
            let root = (_.startsWith(rs.root, "/") ? "" : "/") + rs.root + (_.endsWith(rs.root, "/") ? "" : "/");
            rs.components.keys().forEach((ckey) => {
                let path = root + ckey.substring(2, ckey.indexOf('.vue'));
                let com = rs.components(ckey).default;
                let ro = _.findLast(rs.routes || [], (n) => {
                    return (n.path == path) || (n.path == path + "/");
                }) || {};
                if (ro.path) {
                    ro._appended_ = true;
                } else {
                    ro.path = path;
                }
                if (!ro.component) {
                    ro.component = com;
                }
                result.push(ro);
            });
            let others = _.find(rs.routes || [], (n) => {
                return n._appended_ == undefined;
            }) || [];
            if (others.length > 0) {
                result = result.concat(others);
            }
        } else if (rs.routes && _.isArray(rs.routes) && rs.routes.length > 0) {
            result = result.concat(rs.routes);
        }
    });
    result.forEach(r => {
      if (r.path == Config.pages.home) {
        result.push({
            ...r,
            ...{
              path:"/"
            }
        });
      }
    });
    return result;
})(require.context('./', true, /^\.\/modules\/((?!\/)[\s\S])+\/route\.js$/));

export default [
    ...modules,
    {
        path: '/zjApp',
        component: zjApp
    },
    {
        path: '/ajzApp',
        component: ajzApp
    },
    {
        path: '(.*)',
        component: nofund,
        anonymous: true
    }
];
